package com.service.supply;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.common.spring.SpringMvcUtils;
import com.dao.supply.SupplyBankDao;
import com.model.supply.SupplyBankModel;
import com.model.supply.SupplyUserModel;

@Service("supplyBankService")
public class BankService {
	static final Logger logger = LoggerFactory.getLogger(BankService.class);

	@Autowired
	SupplyBankDao bankDao;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return HttpResult.error("当前会话不存在！");
		}
		SupplyUserModel supplyUserModel = SpringMvcUtils.getSupplyUser();
		if (supplyUserModel == null) {
			return HttpResult.error("用户不存在");
		}
		queryMap.put("infId", supplyUserModel.getInfId());
		List<SupplyBankModel> list = bankDao.selectList(queryMap);
		int count = bankDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String bakCode, String infId) {
		HttpResult httpResult = new HttpResult();
		SupplyBankModel bankModel = bankDao.load(bakCode, infId);
		httpResult.setData(bankModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	public HttpResult save(SupplyBankModel bankModel) {
		if (bankModel == null) {
			return HttpResult.error("参数不能为空");
		}
		if (StringUtils.isNullOrEmpty(bankModel.getBakCode())) {
			return HttpResult.error("银行账号不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;

		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return HttpResult.error("当前会话不存在！");
		}
//		SupplyUserModel supplyUserModel = SpringMvcUtils.getSupplyUser();
		if (StringUtils.isNullOrEmpty(bankModel.getBakCode())) {
			return HttpResult.error("银行账号不能为空！");
		}
//		bankModel.setInfId(supplyUserModel.getInfId());
//		bankModel.setInfName(supplyUserModel.getInforName());

		result = bankDao.update(bankModel);
		if (result == 0) {
			result = bankDao.insert(bankModel);
		} else {
		}
		if (result > 0) {
			httpResult.setMsg("保存成功！");
			httpResult.setData(bankModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("保存失败！");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String bakCode, String infId) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(bakCode) || !StringUtils.isNotNull(infId)) {
			return HttpResult.error("参数不能为空");
		}
		SupplyBankModel model = bankDao.load(bakCode, infId);
		if (model == null) {
			return HttpResult.error("无数据！");
		}
		if (!model.getStatus().equals(Global.NO + "")) {
			return HttpResult.error("非待提交状态，不可删除！");
		}
		int result = bankDao.delete(bakCode, infId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		List<SupplyBankModel> list = bankDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), SupplyBankModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
